package com.caigua.mgp.p;

import com.caigua.mgp.mg.utils.GenUtil;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.Messages;
import com.intellij.openapi.vfs.VirtualFileManager;
import com.intellij.openapi.wm.ToolWindow;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.text.CaseUtils;

import javax.swing.*;
import java.io.File;
import java.util.List;

/**
 * @BelongsProject: mybatis_generator_pluing
 * @BelongsPackage: com.caigua.mgp.p
 * @Author: caigua
 * @CreateTime: 2020-12-09 17:37
 * @Description: todo
 */
public class OptionWindow {
    private JPanel contentPanel;
    private JScrollPane table;
    private JPanel tablePanel;
    private JList tableLiist;
    private JButton codeGeneratorButton;
    private JTextField domainSuffixTextField;
    private JLabel domainSuffixLabel;
    private JLabel modelPackgetLabel;
    private JTextField modelPackagetTextField;
    private JTextField modelProjectTextField;
    private JLabel modelProjectLabel;
    private JTextField mapperPackagetTextField;
    private JTextField sqlMapperPackagetTextField;
    private JTextField mapperProjectTextField;
    private JTextField sqlMapperProjectTextField;
    private JLabel mapperPackagetLabel;
    private JLabel mapperProjectLabel;
    private JLabel sqlMapperPackagetLabel;
    private JLabel sqlMapperProjectLabel;
    private JTextField domainOverPrefixTextField;
    private JLabel domainOverPrefixLabel;
    private JLabel pageParamClassLabel;
    private JTextField pageParamClassTextField;

    private static char[] castD = new char[]{'_'};

    public OptionWindow(ToolWindow toolWindow, Project project) {
        codeGeneratorButton.addActionListener(e -> {
            List<String> selectedValuesList = tableLiist.getSelectedValuesList();
            if(CollectionUtils.isEmpty(selectedValuesList)) {
                Messages.showMessageDialog("请选择需要生成的数据表", "提示信息", null);
            }else {
                if(StringUtils.isAnyBlank(sqlMapperPackagetTextField.getText(), sqlMapperProjectTextField.getText(),
                        modelPackagetTextField.getText(), modelProjectTextField.getText(),mapperPackagetTextField.getText(),
                        mapperProjectTextField.getText())) {
                    Messages.showErrorDialog("生成目录必须配置", "警告信息");
                }
                GenUtil.setPageParamClass(pageParamClassTextField.getText());
                GenUtil.domainOverPrefix.setLength(0);
                if(StringUtils.isNotBlank(domainOverPrefixTextField.getText())) {
                    GenUtil.domainOverPrefix.append(domainOverPrefixTextField.getText());
                }
                GenUtil.getContext().getTableConfigurations().clear();
                StringBuilder domainTable = new StringBuilder();
                for (String table : selectedValuesList) {
                    domainTable.setLength(0);
                    if(table.startsWith(domainOverPrefixTextField.getText())) {
                        domainTable.append(table.substring(domainOverPrefixTextField.getText().length()));
                    }else {
                        domainTable.append(table);
                    }
                    GenUtil.addTableInfo(table, CaseUtils.toCamelCase(StringUtils.isBlank(domainSuffixTextField.getText())  ? domainTable.toString() : domainTable.toString() + "_" + domainSuffixTextField.getText(),  true, castD));
                }
                GenUtil.setXMLMapperConfig(null, sqlMapperPackagetTextField.getText(), project.getBasePath()+ File.separator+sqlMapperProjectTextField.getText() );
                GenUtil.setJavaModelConfig(null, modelPackagetTextField.getText(), project.getBasePath()+ File.separator+modelProjectTextField.getText());
                GenUtil.setJavaMapperConfig(null, mapperPackagetTextField.getText(), project.getBasePath()+ File.separator+mapperProjectTextField.getText() );
                try {
                    GenUtil.generateCode();
                    Messages.showInfoMessage("生成成功！", "提示信息");
                    VirtualFileManager.getInstance().syncRefresh();
                } catch (Throwable t) {
                    Messages.showErrorDialog( t.getMessage(), "生成失败");
                }
            }
        });
    }

    public void loadTableList() {

        tableLiist.setModel(new AbstractListModel() {

            @Override
            public int getSize() {
                return GenUtil.cacheTables.size();
            }

            @Override
            public Object getElementAt(int index) {
//                return CaseUtils.toCamelCase(GenUtil.cacheTables.get(index).getTableName(), true,new char[]{'_'});
                return GenUtil.cacheTables.get(index).getTableName();
            }
        });
    }

    public JPanel getContentPanel() {
        return contentPanel;
    }
}
